﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using CatClient.Util;

namespace CatClient.Message.Spi.Heartbeat.Extend
{
    // Token: 0x0200003E RID: 62
    public class NetworkIO : HeartbeatExtention
    {
        // Token: 0x06000220 RID: 544 RVA: 0x0000851C File Offset: 0x0000671C
        public NetworkIO()
        {
            this._stopwatch = new Stopwatch();
            this._stopwatch.Start();
            this._dict = new Dictionary<string, double>();
            PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
            Dictionary<string, NetworkInterface> dictionary = NetworkIO.GetNetworkInterfaces().ToDictionary((NetworkInterface n) => n.Description.Replace('#', '_').Replace('/', '_').Replace('(', '[').Replace(')', ']'), (NetworkInterface n) => n);
            string[] instanceNames = performanceCounterCategory.GetInstanceNames();
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info("network interfaces: " + string.Join(";", dictionary.Keys));
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info("network categories: " + string.Join(";", instanceNames));
            foreach (string text in instanceNames)
            {
                if (dictionary.ContainsKey(text) && !text.ToLower().Contains("loopback"))
                {
                    NetworkAdapter networkAdapter = new NetworkAdapter(dictionary[text].Name);
                    networkAdapter.NetworkBytesReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", text);
                    networkAdapter.NetworkBytesSend = new PerformanceCounter("Network Interface", "Bytes Sent/sec", text);
                    this._adappterList.Add(networkAdapter);
                }
            }
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info(string.Format("network adapter count: {0}", this._adappterList.Count));
            this._stopwatch.Stop();
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info(string.Format("Heartbeat {0} init finish. {1}ms", "NetworkIO", this._stopwatch.ElapsedMilliseconds));
        }

        // Token: 0x17000083 RID: 131
        // (get) Token: 0x06000221 RID: 545 RVA: 0x000086EE File Offset: 0x000068EE
        public override Dictionary<string, double> Dict
        {
            get
            {
                return this._dict;
            }
        }

        // Token: 0x17000084 RID: 132
        // (get) Token: 0x06000222 RID: 546 RVA: 0x000086F6 File Offset: 0x000068F6
        public override string Id
        {
            get
            {
                return "NetWorkIO";
            }
        }

        // Token: 0x06000223 RID: 547 RVA: 0x00008700 File Offset: 0x00006900
        public override void Refresh()
        {
            this._stopwatch.Restart();
            this._adappterList.ForEach(delegate (NetworkAdapter item)
            {
                this._dict[item.Name + " Bytes Sent/sec"] = (double)item.NetworkBytesSend.NextValue();
                this._dict[item.Name + " Bytes Received/sec"] = (double)item.NetworkBytesReceived.NextValue();
            });
            this._stopwatch.Stop();
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info(string.Format("Heartbeat {0} refresh finish. {1}ms", "CpuInfo", this._stopwatch.ElapsedMilliseconds));
        }

        // Token: 0x06000224 RID: 548 RVA: 0x0000876C File Offset: 0x0000696C
        public static List<NetworkInterface> GetNetworkInterfaces()
        {
            List<NetworkInterface> list = new List<NetworkInterface>();
            NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info("network all interfaces: " + string.Join("", from i in allNetworkInterfaces
                                                                                                                                   select string.Format("【type: {0}, status: {1}】", i.NetworkInterfaceType, i.OperationalStatus)));
            foreach (NetworkInterface networkInterface in allNetworkInterfaces)
            {
                if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet && networkInterface.OperationalStatus == OperationalStatus.Up)
                {
                    try
                    {
                        if (networkInterface.GetIPProperties().GetIPv4Properties() != null)
                        {
                            list.Add(networkInterface);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            return list;
        }

        // Token: 0x040000DE RID: 222
        private readonly Stopwatch _stopwatch;

        // Token: 0x040000DF RID: 223
        private readonly Dictionary<string, double> _dict;

        // Token: 0x040000E0 RID: 224
        private readonly List<NetworkAdapter> _adappterList = new List<NetworkAdapter>();
    }
}
